package com.yxq.mdt.app.modules.bottomsheet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-10-30
 * @describe 选择星座
 */
public class PersonalAgeSheetDialogFragment extends BottomSheetDialogFragment {
    Unbinder unbinder;
    @BindView(R.id.dialog_tv_age)
    TextView dialogTvAge;
    @BindView(R.id.dialog_tv_constellation)
    TextView dialogTvConstellation;
    @BindView(R.id.dialog_check_box_confidentiality)
    CheckBox dialogCheckBoxConfidentiality;
    @BindView(R.id.dialog_date_picker)
    DatePicker dialogDatePicker;
    @BindView(R.id.dialog_tv_save)
    TextView dialogTvSave;
    String selectedDay;
    String constellation;
    int age;
    String birthday;
    int birthdayShow;

    public PersonalAgeSheetDialogFragment(String birthday, int birthdayShow) {
        this.birthday = birthday;
        this.birthdayShow = birthdayShow;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.personal_age_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    private void initViews() {
        dialogCheckBoxConfidentiality.setChecked(birthdayShow == 1);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int _year = calendar.get(Calendar.YEAR);
        int month = 0;
        int day;
        if (birthday != null && birthday.split("-") != null && birthday.split("-").length >= 3) {
            String[] date = birthday.split("-");
            _year = Integer.parseInt(date[0]);
            month = Integer.parseInt(date[1]) - 1;
            day = Integer.parseInt(date[2]);
        } else {
            _year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DAY_OF_MONTH);
        }

        constellation = getConstellation(month + 1, day);
        dialogTvConstellation.setText(constellation);
        dialogTvAge.setText((year - _year) + "岁");
        age = (year - _year);
        dialogDatePicker.setMaxDate(new Date().getTime());
        selectedDay = _year + "-" + ((month + 1) < 10 ? "0" + (month + 1) : (month + 1)) + "-" + (day < 10 ? "0" + day : day);
        dialogDatePicker.init(_year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int _year, int monthOfYear, int dayOfMonth) {
                monthOfYear = monthOfYear + 1;
                selectedDay = _year + "-" + (monthOfYear < 10 ? "0" + monthOfYear : monthOfYear) + "-" + (dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth);
                constellation = getConstellation(monthOfYear, dayOfMonth);
                dialogTvConstellation.setText(constellation);
                dialogTvAge.setText((year - _year) + "岁");
                age = (year - _year);
            }
        });
    }

    private String getConstellation(int month, int day) {
        String[] astro = new String[]{"摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座",
                "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"};
        int[] arr = new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};// 两个星座分割日
        int index = month;
        // 所查询日期在分割日之前，索引-1，否则不变
        if (day < arr[month - 1]) {
            index = index - 1;
        }
        // 返回索引指向的星座string
        return astro[index];
    }


    @OnClick(R.id.dialog_tv_save)
    public void onViewClicked() {
        dismiss();
        if (personalAgeSheetDialogFragmentInterface != null) {
            personalAgeSheetDialogFragmentInterface.date(selectedDay, age, constellation, dialogCheckBoxConfidentiality.isChecked());
        }
    }

    PersonalAgeSheetDialogFragmentInterface personalAgeSheetDialogFragmentInterface;

    public void setPersonalAgeSheetDialogFragmentInterface(PersonalAgeSheetDialogFragmentInterface personalAgeSheetDialogFragmentInterface) {
        this.personalAgeSheetDialogFragmentInterface = personalAgeSheetDialogFragmentInterface;
    }

    public interface PersonalAgeSheetDialogFragmentInterface {
        void date(String date, int age, String constellation, boolean confidentiality);
    }
}
